<template>
	<view class="add page c00">
		<view class="bgf7 p12">
			<view class="bgff px14 pb10">

				<view class="flex flexC borb py12">
					<text class="flex1">酒店名字</text>
					<input v-model="name" class="aliR py3" placeholder="请输入酒店的名字" />
					<text class="arrowR ml3"></text>

				</view>
				<view class="flex flexC borb py12">
					<text class="flex1">手机号</text>
					<input v-model="mobile" type="digit" class="bgf6  aliR r40 px12 py3" placeholder="请输入手机号" />
					<text class="arrowR ml3"></text>

				</view>
				<view class="flex flexC borb py12" @click="clickLocation">
					<text class="flex1">地址</text>
					<input v-model="address" disabled="disable" type="text" class="py3 aliR" placeholder="请输入地址" />
					<text class="arrowR ml3"></text>
				</view>
				
				<view class="flex flexC  py12" @click="updateLogo">
					<text class="flex1">门头LOGO</text>
					<image class="w48 " :src="logo_url" mode="aspectFill"></image>
					<text class="arrowR ml3"></text>
				</view>


			</view>
			<view class="flex flexC flexJC py20">
				<view class="aliC py12 w-17 bg41 r40" @click="save">保存</view>
			</view>



		</view>




	</view>
</template>

<script>
	export default {
		data() {
			return {
				address: '',
				lat: '',
				lng: '',
				province: '',
				city: '',
				area: '',
				name: '',
				mobile: '',
				logo_url:'',
				logo_path:''

			};
		},
		onLoad(option) {
		
		},

		onShow() {

		},
		methods: {

			clickLocation() {
				let that = this;
				uni.chooseLocation({

					fail: function(res) {
						console.info(res);
					},
					success: function(res) {
						console.info(res)
						that.lat = res.latitude;
						that.lng = res.longitude;

						let regex =
							"(?<province>[^省]+省|.+自治区)(?<city>[^自治州]+自治州|[^市]+市|[^盟]+盟|[^地区]+地区|.+区划)(?<county>[^市]+市|[^县]+县|[^旗]+旗|.+区)?(?<town>[^区]+区|.+镇)?(?<village>.*)"
						let newAddress = res.address.match(regex);
						let group = newAddress.groups;


						that.province = group.province;
						that.city = group.city;
						if (group.county != null) {
							that.area = group.county;
						}
						if (group.town != null) {
							that.area = group.county;
						}
						that.address = that.province + " " + that.city + " " + that.area + group.village;
						that.updateAddress();
						console.log('位置名称：' + res.name);
						console.log('详细地址：' + res.address);
						console.log('纬度：' + res.latitude);
						console.log('经度：' + res.longitude);
					}
				})
			},

		

			updateLogo() {
				let that = this;
				this.uploadType = "image";
				uni.chooseImage({
					count: 1, //默认9
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ["camera", "album"], //从相册选择
					success: function(res) {
						//console.log(JSON.stringify(res.tempFilePaths));
						let images = res.tempFilePaths; //非APP平台不支持自定义压缩,暂时没有处理,可通过uni-app上传组件的sizeType属性压缩
						for (var i = 0; i < images.length; i++) {
							let tImg = images[i];
							that.upload(tImg, 'logo');
						}
					}
				});
			},



			upload(img, type) {

				let that = this;
				uni.showLoading({
					title: "上传中"
				})
				uni.uploadFile({
					url: this.$uploadUrl,
					filePath: img,
					name: 'file',
					success: (uploadFileRes) => {

						uni.hideLoading();
						var res = uploadFileRes.data;

						// 根据接口具体返回格式   赋值具体对应url
						var url = JSON.parse(res).data.fullurl;
						var uploadUrl = JSON.parse(res).data.url;
						that.logo_url = url;
						that.logo_path = uploadUrl;

					}
				});

			},






			save() {

				if (this.name == '') {

					uni.showToast({
						title: "请填写名字",
						icon: 'none'
					})
					return;
				}


				if (this.mobile == '') {

					uni.showToast({
						title: "请填写手机号",
						icon: 'none'
					})
					return;
				}

			

				if (this.address == "") {

					uni.showToast({
						title: "请选择地址",
						icon: 'none'
					})
					return;
				}


				if (this.logo_path == "") {

					uni.showToast({
						title: "请上传logo",
						icon: 'none'
					})
					return;
				}

			
				let param = {
					name: this.name,
					mobile:this.mobile,
					address:this.address,
					lat:this.lat,
					lng:this.lng,
					logo:this.logo_path
				};
				let that = this;
				this.$api.hotelAdd(param).then(res => {

					if (res.code == 1) {
						uni.showModal({
							title: "添加提示",
							content: "添加成功",
							showCancel: false,
							complete() {
								uni.navigateBack({
									delta: 1
								})
							}
						})
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
					}
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.add {
		.w48 {
			width: 96rpx;
			height: 96rpx;
		}

		.imgbox {
			width: 164rpx;
			height: 164rpx;
			background-color: #E8E8E8;
			position: relative;

			image {
				width: 100%;
				height: 100%;
			}

			.del {
				width: 36rpx;
				height: 36rpx;
				background: rgba(181, 180, 180, .8);
				position: absolute;
				top: -12rpx;
				right: -12rpx;
			}
		}
	}

	.popup-content2 {
		background: #FFFFFF;

		.popup-btn {
			width: 100%;
			height: 100rpx;
			line-height: 100rpx;
			text-align: center;
			border-bottom: 1px solid #f5f5f5;
			background: #FFFFFF;
		}

		.button {
			width: 100%;
			height: 100rpx;
			line-height: 100rpx;
			text-align: center;
			border-bottom: 1px solid #f5f5f5;
			background: #FFFFFF;
			font-size: 11pt;
		}

		button::after {
			border: none;
		}

		.pt20 {
			border-bottom: 20rpx solid #f5f5f5;
		}
	}
</style>